package com.mocaris.android.gank.view.wanandroid

import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.mocaris.android.gank.MainActivity
import com.mocaris.android.gank.R
import kotlinx.android.synthetic.main.fragment_android_main.*


/**

 */
class AndroidMainFragment : Fragment(), ViewPager.OnPageChangeListener,
    BottomNavigationView.OnNavigationItemSelectedListener {


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_android_main, container, false)
    }

    companion object {
        val menItems = arrayOf(
            R.id.menu_home,//首页
            R.id.menu_sysTree,//知识体系
            R.id.menu_navi,//导航页
            R.id.menu_mine//我的
        )

        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            AndroidMainFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

    private val fragments = arrayListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fragments.add(AndroidHomeFragment.newInstance())
        fragments.add(SystemTreeFragment.newInstance())
        fragments.add(AndroidHomeFragment.newInstance())
        fragments.add(AndroidHomeFragment.newInstance())
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        bnv_bar.setOnNavigationItemSelectedListener(this)
        vp_home.offscreenPageLimit = 3
        vp_home.addOnPageChangeListener(this)
        vp_home.adapter = object : FragmentPagerAdapter(childFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

            override fun getCount(): Int {
                return fragments.size
            }
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        item.isChecked = true
        when (item.itemId) {
            menItems[0] -> {
                vp_home?.currentItem = 0
            }
            menItems[1] -> {
                vp_home?.currentItem = 1
            }
            menItems[2] -> {
                vp_home?.currentItem = 2
            }
            menItems[3] -> {
                vp_home?.currentItem = 3
            }
        }
        (activity is MainActivity).let {
            if (it) {
                (activity as MainActivity).supportActionBar?.title = item.title
            }
        }
        return false
    }

    override fun onPageSelected(position: Int) {
        bnv_bar?.selectedItemId = menItems[position]
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

}
